local Lua__kamenjiayu = fk.CreateSkill({
  name = "Lua__kamenjiayu",
  tags = { Skill.Permanent },
})


Fk:loadTranslationTable{
  ["Lua__kamenjiayu"] = "假面驾驭",
  [":Lua__kamenjiayu"] = "持恒技，共鸣技，当一名角色濒死时，你可以从剩余武将牌堆中随机获得一张驾驭卡，准备阶段/结束阶段，你可以选择一张驾驭卡作为你的主将或副将。",
  ["@&Lua__kamenjiayu_cards"] = "驾驭卡",
}



local function acquireRidingCard(player)
  local room = player.room
  local generals = room:getNGenerals(1)
  if #generals == 0 then return end
  local general = generals[1]
  local riding_cards = player:getTableMark("@&Lua__kamenjiayu_cards") or {}
  table.insertIfNeed(riding_cards, general)
  room:setPlayerMark(player, "@&Lua__kamenjiayu_cards", riding_cards)
  room:notifySkillInvoked(player, "Lua__kamenjiayu")
end


Lua__kamenjiayu:addEffect(fk.EnterDying, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(Lua__kamenjiayu.name) and not player.dead and
    (player.general == "DIEND" or player.deputyGeneral == "DIEND")
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askToSkillInvoke(player, {
      skill_name = self.name,
      prompt = "是否获得一张驾驭卡？",
    })
  end,
  on_use = function(self, event, target, player, data)
    acquireRidingCard(player)
  end,
})



Lua__kamenjiayu:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self.name) and player == target and
    (player.general == "DIEND" or player.deputyGeneral == "DIEND") then 
      if player.phase ~= Player.Start and player.phase ~= Player.Finish then return false end
      local riding_cards = player:getTableMark("@&Lua__kamenjiayu_cards") or {}
      return #riding_cards > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local phase_name = player.phase == Player.Start and "回合开始" or "回合结束"
    local prompt = phase_name .. "时，是否使用一张驾驭卡更换主将或副将？"
    return room:askToSkillInvoke(player, {
      skill_name = self.name,
      prompt = prompt,
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local riding_cards = player:getTableMark("@&Lua__kamenjiayu_cards") or {}
    if #riding_cards == 0 then return end
    local generals_table = {}
    for _, general in ipairs(riding_cards) do
      table.insert(generals_table, general)
    end
    local chosen_general = room:askToChooseGeneral(player, {
      generals = generals_table,
      n = 1,
      reason = "Lua__kamenjiayu",
      no_convert = true,
    })
    if type(chosen_general) == "table" then
      chosen_general = chosen_general[1]
    end
    local role_choices = {"作为主将", "作为副将"}
    local role_choice = room:askToChoice(player, {
      choices = role_choices,
      skill_name = self.name
    })
    table.removeOne(riding_cards, chosen_general)
    room:setPlayerMark(player, "@&Lua__kamenjiayu_cards", riding_cards)
    if role_choice == "作为主将" then
      if player.general ~= "" then
        table.insert(riding_cards, player.general)
        room:setPlayerMark(player, "@&Lua__kamenjiayu_cards", riding_cards)
      end
      room:changeHero(player, chosen_general, false, false, true, true)
    else
      if player.deputyGeneral ~= "" then
        table.insert(riding_cards, player.deputyGeneral)
        room:setPlayerMark(player, "@&Lua__kamenjiayu_cards", riding_cards)
      end
      room:changeHero(player, chosen_general, false, true, true, true)
    end
  end,
})



return Lua__kamenjiayu